package com.zoweunion.mechanic.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.EngineServerDao;
import com.zoweunion.mechanic.plugins.redis.RedisUtil;
import com.zoweunion.mechanic.service.EngineServerService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("EngineServerService")
public class EngineServerServiceImpl implements EngineServerService {

    @Autowired
    private EngineServerDao engineServerDao;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 服务商列表
     */
    @Override
    public List<Map<String,Object>> selectServerList(Map<String, Object> params) throws MyException {
        List<Map<String, Object>> listw = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> servers = engineServerDao.selectListByPId(params.get("s_id").toString());
//        List<Map<String, Object>> servers = engineServerDao.selectListByPId("ac15bd181d2f4704a3f4029b0a104535");
        servers.parallelStream().forEach(server -> {
            server.put("province_code", Orgin.getString(params, "province_code"));
            server.put("city_code", Orgin.getString(params, "city_code"));
            server.put("district_code", Orgin.getString(params, "district_code"));
            Map<String, Object> serverInfo = engineServerDao.selectServerBySId(server);
            List<Map<String, Object>> list2 = new ArrayList<>();
            if (serverInfo != null && !serverInfo.toString().equals("") && serverInfo.containsKey("id")) {
                if (redisUtil.exists(serverInfo.get("id").toString() + "_things_info")) {
                    list2 = redisUtil.getList(serverInfo.get("id").toString() + "_things_info");
                } else {
                    list2 = engineServerDao.selectThingsBySId(serverInfo);
                }
                serverInfo.put("thingsCount", list2.size());
                listw.add(serverInfo);
            }
        });
        return listw;
    }

    /**
     * 服务商详情
     */
    @Override
    public Map<String, Object> selectServerInfoList(Map<String, Object> params) {
        Map<String, Object> serverInfoList = engineServerDao.selectServerInfoList(params);
        List<Map<String, Object>> things = engineServerDao.selectThingsBySId(serverInfoList);
        serverInfoList.put("carsCount", things.size());
        serverInfoList.put("online", 0);
        serverInfoList.put("fault", 0);
        serverInfoList.put("locked", 0);
        things.forEach(item->{
            if (Orgin.getString(item, "things_id") != null) {
                Map<String, Object> status = engineServerDao.selectStatusByThings(Orgin.getString(item, "things_id"));
                if (status != null) {
                    if (Orgin.getIntValue(status, "status") == 1) {
                        serverInfoList.put("online", Orgin.getIntValue(serverInfoList, "online") + 1);
                    }
                    if (Orgin.getIntValue(status, "fault_status") == 1) {
                        serverInfoList.put("fault", Orgin.getIntValue(serverInfoList, "fault") + 1);
                    }
                    if (Orgin.getIntValue(status, "lock_status") == 1) {
                        serverInfoList.put("locked", Orgin.getIntValue(serverInfoList, "locked") + 1);
                    }
                }
            }
        });
        serverInfoList.put("outline", things.size()-Orgin.getIntValue(serverInfoList, "online"));
        return serverInfoList;
    }

    /**
     * 服务商车辆列表
     */
    @Override
    public PageInfo selectServerThingsList(Map<String, Object> params) {
        Integer pageSize = Orgin.getInteger(params, "pageSize");
        Integer currentPage = Orgin.getInteger(params, "currentPage");
        PageHelper.startPage(currentPage, pageSize);
        List<Map<String, Object>> serverThingsList = new ArrayList<>();
        serverThingsList = engineServerDao.selectServerThingsList(params);
        serverThingsList.forEach(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("s_id", Orgin.getString(params, "s_id"));
            map.put("id", Orgin.getString(item, "id"));
            String name = engineServerDao.selectUserNameById(map);
            if (name != null) {
                item.put("user_name", name);
            } else {
                item.put("user_name", "暂无");
            }
        });
        PageInfo mapPageInfo = new PageInfo<>(serverThingsList);
        return mapPageInfo;
    }
}
